// Sprite Frame Animator
// (c) jimon game studio

#ifndef JEH_JESPRITEFRAMEANIMATOR
#define JEH_JESPRITEFRAMEANIMATOR

#include "ISceneManager.h"
#include "IAnimator.h"
#include "ISpriteSceneNode.h"
#include "ITimer.h"

namespace je
{
	namespace scene
	{
		//! Sprite Frame Animator
		class jeSpriteFrameAnimator:public je::scene::IAnimator
		{
		protected:
			f32 LastTime;
			f32 DeltaTime;
			s8 Delta;
		public:
			//! Constructor
			jeSpriteFrameAnimator()
				:IAnimator("SpriteFrameAnimator"),LastTime(0),DeltaTime(0),Delta(1)
			{
			}

			//! Destructor
			~jeSpriteFrameAnimator()
			{
			}

			//! Set Delta Time
			void SetDeltaTime(f32 NewDeltaTime)
			{
				DeltaTime = NewDeltaTime;
			}

			//! Get Delta Time
			f32 GetDeltaTime()
			{
				return DeltaTime;
			}

			//! Set Delta
			void SetDelta(s8 NewDelta)
			{
				Delta = NewDelta;
			}

			//! Get Delta
			s8 GetDelta()
			{
			}

			//! On Update
			void OnUpdate()
			{
				ISpriteSceneNode * Node = static_cast<ISpriteSceneNode*>(ParentNode);

				f32 Time = (f32)core::GetTimer()->GetTime();
				if((Time - LastTime)>DeltaTime)
				{
					LastTime = Time;
					Node->SetFrame(Node->GetFrame()+Delta);
					if(Node->GetFrame() >= Node->GetFrameCount())
						Node->SetFrame(0);
					if(Node->GetFrame() < 0)
						Node->SetFrame(Node->GetFrameCount()-1);
				}

				IAnimator::OnUpdate();
			}

			//! Serialize
			void Serialize(core::ISerializeStream * SerializeStream)
			{
				JE_SS_BEGIN(SpriteFrameAnimator)
				JE_SS(DeltaTime)
				JE_SS_END(SpriteFrameAnimator)

				if(JE_SS_LOAD)
					LastTime = 0;

				IAnimator::Serialize(SerializeStream);
			}
		};
	}
}

#endif
